var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';

import {$wuxToptips} from '../../components/wux';
import WxValidate from '../../assets/plugins/WxValidate';
import { $wuxDialog } from '../../components/wux';


var app = getApp();
var that,wxbjdays;

var _nowdate = new Date();
var strdate = util.formatTime(_nowdate);
var strtime = strdate.substr(11);
strdate = strdate.substr(0,10);

//获取应用实例
var app = getApp()
Page({
  data: {
    user: {},
    nowdate:_nowdate,
    form: {
      key: "", 
      ldate: strdate, 
      time:strtime
    },
    tid:0
  },
  onLoad: function (options) {
    that = this;
    wx.setNavigationBarTitle( { title: "授权码表单" });
    
    if( !util.judge.isNull( options.id) ){
      that.data.tid = parseInt(options.id);
    }
   
    that.initValidate();
    that.loadData();

  },
  loadData: function () {
    util.showLoading();
    app.getUserInfo()
    .then(res => {
      that.setData({
        user:res
      });
    })
    .then(res=>{
      if(that.data.tid == 0){
        that.setData({
          'form.key':"提交保存,生成授权码"
        });
        throw util.stop();
      }

      var url = config.url_sqm_get;
      var params = {
        id:that.data.tid
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {

        that.setData({form:res});
        
        that.data.tid = parseInt(res.id);

        that.setData({
          'form.key':res.key,
          'form.ldate':res.ldate.substr(0,10),
          'form.time':res.ldate.substr(11,5)
        });


        //处理时间
        //
    })
    .catch(res => {
      util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });
  },
  showToptips(error) {
    const hideToptips = $wuxToptips.show({
      timer: 3000,
      text: error.msg || '请填写正确的字段',
      success: () => console.log('toptips', error)
    })
  },
  submitForm(e) {
    const data = e.detail.value

    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0]
      this.showToptips(error)
      return false
    }

    util.showLoading();

    var url = config.url_sqm_edit;
   
    data.id =that.data.tid;
    data.ldate = data.ldate + " " + data.time;
 
    var params = data;

    wxRequest.postRequest(url, params)
    .then(res => {
        that.data.tid = res.id;
        that.setData({
          'form.key':res.key
        });

        util.alert("提交成功");

        //刷新列表
        var pages = getCurrentPages();
        var page= pages[pages.length - 2];
        page.requestData();

    })
    .catch(res => {
        util.alert(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });

  },
  initValidate() {
      this.WxValidate = new WxValidate(
        {
        key: {
          required: true, 
        },
        ldate: {
          required: true, 
        },
      }
      , 
      {
         name: {
          required: "请输入授权码", 
        },
        ldate: {
          required: "请输入截止时间", 
        }
      }
    );
  },
  bindldate: function(e) {
    this.setData({
      'form.ldate': e.detail.value
    })
  },
  bindtime: function(e) {
    this.setData({
      'form.time': e.detail.value
    })
  },

  
})